package com.woniuxy.mapper;



import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.entity.Shop;
import com.woniuxy.entity.User;
import com.woniuxy.provider.UserProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 用户Mapper接口
 */
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据id删除用户,逻辑删除
     * @author wmy
     */
    @Update("update gfr_user set status='n' where id=#{id}")
    void deleteById(int id);

    /**
     * 修改用户信息
     * @param user
     */
    @UpdateProvider(type = UserProvider.class,method = "update")
    void update(User user);

    @Select("select * from gfr_user where status='y'")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "email", property = "email"),
            @Result(column = "status", property = "status")
    })
    List<User> getAll();

    /**
     * 根据用户名查询用户
     * @param account
     * @return
     */
    @Select("select * from gfr_user where name=#{account}")
    User getByAccount(String account);

    /**
     * 动态查找用户
     * @param user
     * @return
     */
    @SelectProvider(type = UserProvider.class, method = "select")
    List<User> getByCondition(User user);

    @Update("update gfr_user set password=#{password} where name=#{name}")
    void updateByName(User user);

    /**
     * 查询一个name的数量
     * @param name
     * @return
     */
    @Select("select count(*) from gfr_user where name=#{name}")
    int getNameCount(String name);

    /**
     * 查询一个邮箱的数量
     * @param email
     * @return
     */
    @Select("select count(*) from gfr_user where email=#{email}")
    int getEmileCount(String email);


    /**
     * 通过用户id查找用户收藏的店铺
     * @param id
     * @return
     */
    @Select("SELECT s.* FROM  gfr_shop s,gfr_user_shop us WHERE s.id=us.shopId AND us.userId=#{id}")
    List<Shop> getShopByUserId(int id);

    /**
     * 通过查找用户id和对应的商铺id来判断用户是否将店铺已经收藏
     * @param userId
     * @param shopId
     * @return
     */
    @Select("select count(*) from gfr_user_shop where userId=#{userId} and shopId=#{shopId}")
    int collectIsExist(int userId,int shopId);

    /**
     * 店铺收藏添加
     * @param userId
     * @param shopId
     */
    @Insert("insert into gfr_user_shop(userId,shopId) values(#{userId},#{shopId})")
    void collectShop(int userId, int shopId);

    /**
     * 取消收藏店铺
     * @param userId
     * @param shopId
     */
    @Delete("delete from gfr_user_shop where userId=#{userId} and shopId=#{shopId}")
    void noCollectShop(int userId, int shopId);
}
